﻿using K9Nano.IM.Areas.Chat;
using K9Nano.IM.LayIM;
using K9Nano.LayIM;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Mvc;
using WalkingTec.Mvvm.TagHelpers.LayUI;

namespace K9Nano.IM
{
    public class Program
    {

        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateWebHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                //.ConfigureAppConfiguration((hostContext, builder) =>
                //{
                //    var path = Path.Combine(Directory.GetCurrentDirectory(), "config");
                //    builder.SetBasePath(path)
                //        .AddJsonFile("appsettings.json", false, true)
                //        .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", true, true)
                //        .AddCommandLine(args)
                //        .AddEnvironmentVariables();

                //})
                .ConfigureLogging((hostingContext, logging) =>
                {
                    logging.ClearProviders();
                    logging.AddConsole();
                    logging.AddWTMLogger();
                })
                 .ConfigureWebHostDefaults(webBuilder =>
                 {
                     webBuilder.ConfigureServices((webBuilderContext, services) =>
                     {
                         services.AddFrameworkService(webHostBuilderContext: webBuilderContext);
                         services.AddLayui();
                         services.AddSwaggerGen(c =>
                         {
                             c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
                             var bearer = new OpenApiSecurityScheme()
                             {
                                 Description = "JWT Bearer",
                                 Name = "Authorization",
                                 In = ParameterLocation.Header,
                                 Type = SecuritySchemeType.ApiKey

                             };
                             c.AddSecurityDefinition("Bearer", bearer);
                             var sr = new OpenApiSecurityRequirement();
                             sr.Add(new OpenApiSecurityScheme
                             {
                                 Reference = new OpenApiReference
                                 {
                                     Type = ReferenceType.SecurityScheme,
                                     Id = "Bearer"
                                 }
                             }, new string[] { });
                             c.AddSecurityRequirement(sr);
                         });

                         services.AddChat();

                         services.AddLayIM(o =>
                             {
                             })
                             .AddFileStore<ChatFileStore>()
                             .AddMessageStore<ChatMessageStore>()
                             .AddFriendStore<ChatFriendStore>()
                             .AddRoomStore<ChatRoomStore>()
                             .AddGroupProvider<ChatGroupProvider>()
                             .AddDefaultFilePolicy()
                             .AddMemoryChatAppOnlineStaffCache()
                             ;

                         services.Configure<ForwardedHeadersOptions>(options =>
                         {
                             options.ForwardedHeaders = ForwardedHeaders.All;
                             options.KnownNetworks.Clear();
                         });
                     });

                     webBuilder.Configure(x =>
                     {
                         x.UseForwardedHeaders();

                         var configs = x.ApplicationServices.GetRequiredService<Configs>();
                         if (configs.IsQuickDebug)
                         {
                             x.UseSwagger();
                             x.UseSwaggerUI(c =>
                             {
                                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "IM V1");
                             });
                         }
                         x.UseFrameworkService();

                         //x.UseEndpoints(endpoints =>
                         //{
                         //    endpoints.MapHub<ChatHub>("/chathub", options =>
                         //    {
                         //        options.Transports = HttpTransportType.WebSockets | HttpTransportType.LongPolling;
                         //    });
                         //});

                         x.UseLayIMRazor();
                     });
                 }
                 );
        }
    }
}
